var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
var tempid;
// 鼠标左键弹起
handler.setInputAction(function (movement) {
    var pickedObjects = scene.drillPick(movement.position);
    if (Cesium.defined(pickedObjects)) {
        var pickedObject = null;
        var length = pickedObjects.length;
        for (var i = 0; i < length; ++i) {
            if (pickedObjects[i].content == undefined) {
                pickedObject = pickedObjects[i];
                break;
            }
        }
        if (pickedObject != null) {
            // var description = pickedObject.id.description.getValue();
        }


    }
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);


// 鼠标移动事件
var oldScale = 1;
var oldColor = Cesium.Color.YELLOW.withAlpha(0.3);
var entiyChanged;
var isChanged = false;
handler.setInputAction(function (movement) {
    var ray = viewer.camera.getPickRay(movement.endPosition);
    var position = viewer.scene.globe.pick(ray, viewer.scene);
    if (Cesium.defined(position)) {
        // var cartesian = viewer.camera.pickEllipsoid(movement.endPosition, viewer.scene.globe.ellipsoid);
        //if (cartesian) {
        //var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
        var cartographic = Cesium.Ellipsoid.WGS84.cartesianToCartographic(position);
        var longitudeString = Cesium.Math.toDegrees(cartographic.longitude).toFixed(6);
        var latitudeString = Cesium.Math.toDegrees(cartographic.latitude).toFixed(6);
        var heightString = Math.ceil(viewer.camera.positionCartographic.height);

        //显示经纬度
        $("#lonlat").text("经度：" + longitudeString + ', ' + "纬度：" + latitudeString + ',' + "高程：" + heightString + '');

    } else {
    }

    if (toolOp != 0)//漫游模式才继续
        return;

    var pickedObject = scene.pick(movement.endPosition);

    OutEntity();

    if (Cesium.defined(pickedObject)) {

        if (!Cesium.defined(pickedObject.id))
            return;

        if (pickedObject.id.dataid == undefined)
            return;

        if (Cesium.defined(pickedObject.id.billboard)) {

            if (!Cesium.defined(pickedObject.id.billboard.scale)) {
                oldScale = 1;
                pickedObject.id.billboard.scale = oldScale;
            }
            if (isChanged == false) {
                oldScale = pickedObject.id.billboard.scale.getValue();
                pickedObject.id.billboard.scale = oldScale * 1.3;
            }
        }

        else if (Cesium.defined(pickedObject.id.point)) {

            if (!Cesium.defined(pickedObject.id.point.pixelSize)) {
                oldScale = 1;
                pickedObject.id.point.pixelSize = 1;
            }
            if (isChanged == false) {
                oldScale = pickedObject.id.point.pixelSize.getValue();
                pickedObject.id.point.pixelSize = oldScale * 1.3;
            }
        }

        else if (Cesium.defined(pickedObject.id.polyline)) {

            if (!Cesium.defined(pickedObject.id.polyline.width)) {
                oldScale = 1;
                pickedObject.id.polyline.width = 1;
            }
            if (isChanged == false) {
                oldScale = pickedObject.id.polyline.width.getValue();
                pickedObject.id.polyline.width = oldScale * 1.3;
            }

        }

        else if (Cesium.defined(pickedObject.id.polygon)) {

            if (!Cesium.defined(pickedObject.id.polygon.material)) {
                pickedObject.id.polygon.material = oldColor;
            }
            if (isChanged == false) {
                pickedObject.id.polygon.material = Cesium.Color.RED.withAlpha(0.3);
            }

        }

        else if (Cesium.defined(pickedObject.id.wall)) {

            if (!Cesium.defined(pickedObject.id.wall.outlineWidth)) {
                oldScale = 1;
                pickedObject.id.wall.outlineWidth = 1;
            }
            if (isChanged == false) {
                oldScale = pickedObject.id.wall.outlineWidth.getValue();
                pickedObject.id.wall.outlineWidth = oldScale * 1.3;
            }
        }

        else if (Cesium.defined(pickedObject.id.polylineVolume)) {

            if (!Cesium.defined(pickedObject.id.polylineVolume.outlineColor)) {
                oldColor = Cesium.Color.YELLOW.withAlpha(0.3);
                pickedObject.id.polylineVolume.outlineColor = oldColor;
            }
            else if (isChanged == false) {
                oldColor = pickedObject.id.polylineVolume.outlineColor.getValue();
                pickedObject.id.polylineVolume.outlineColor = Cesium.Color.RED;
            }
        }

        else if (Cesium.defined(pickedObject.id.model)) {

            if (!Cesium.defined(pickedObject.id.model.scale)) {
                oldScale = 1;
                pickedObject.id.model.scale = 1;
            }
            else if (isChanged == false) {
                oldScale = pickedObject.id.model.scale.getValue();
                pickedObject.id.model.scale = oldScale * 1.3;
            }

        }

        entiyChanged = pickedObject.id;
        isChanged = true;

    }

}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

handler.setInputAction(function (movement) {
    if (toolOp != 0)//漫游模式才继续
        return;
    var ray = viewer.camera.getPickRay(movement.position);
    var position = viewer.scene.globe.pick(ray, viewer.scene);
    if (Cesium.defined(position)) {

        ClickEntity(movement.position);
    }

}, Cesium.ScreenSpaceEventType.LEFT_UP);


//鼠标右键事件
handler.setInputAction(function (movement) {
    var ray = viewer.camera.getPickRay(movement.position);
    var position = viewer.scene.globe.pick(ray, viewer.scene);
    if (Cesium.defined(position)) {
        var entity = getEntity(movement.position);
        if (entity != undefined) {

            showMapRMenu(movement.position.x + 20, movement.position.y, entity);
        } else {
            hideMapRMenu();
        }
    } else {

    }
}, Cesium.ScreenSpaceEventType.RIGHT_DOWN);

function OutEntity() {
    if (isChanged == true) {
        if (Cesium.defined(entiyChanged)) {

            if (Cesium.defined(entiyChanged.billboard))
                entiyChanged.billboard.scale = oldScale;

            else if (Cesium.defined(entiyChanged.point)) {
                entiyChanged.point.pixelSize = oldScale;
            }

            else if (Cesium.defined(entiyChanged.polyline)) {
                entiyChanged.polyline.width = oldScale;
            }

            else if (Cesium.defined(entiyChanged.polygon)) {
                entiyChanged.polygon.material = oldColor;
            }

            else if (Cesium.defined(entiyChanged.wall)) {
                entiyChanged.wall.outlineWidth = oldScale;
            }

            else if (Cesium.defined(entiyChanged.polylineVolume)) {
                entiyChanged.polylineVolume.outlineColor = oldColor;
            }

            else if (Cesium.defined(entiyChanged.model)) {
                entiyChanged.model.scale = oldScale;
            }
            entiyChanged = null;
            isChanged = false;
            oldScale = 1;
        }
    }
}
function ClickEntity(mousePosition) {
    var pickedObjects = scene.drillPick(mousePosition);
    var pickedObject;

    if (Cesium.defined(pickedObjects)) {
        var length = pickedObjects.length;
        for (var i = 0; i < length; ++i) {
            if (pickedObjects[i].content == undefined) {
                pickedObject = pickedObjects[i];
                break;
            }
        }
    }
    if (Cesium.defined(pickedObject)) {

        if (!Cesium.defined(pickedObject.id))//3DTiles
            return;

        if (pickedObject.id == undefined)
            return;

        var description = pickedObject.id.description;
        if (description != undefined) {
            var c = new Cesium.Cartesian2(mousePosition.x, mousePosition.y);
            //屏幕坐标转世界坐标
            var ray = viewer.camera.getPickRay(c);
            var Checkposition = viewer.scene.globe.pick(ray, viewer.scene);


            tempid = pickedObject.id.dataid;

            $('#popup-content').html("<a href='#' onclick='showAllInfo(tempid)'>" + pickedObject.id.name + "</a>" + "<br>" + description);
            $('#popup').show();
            $("#popup").css("left", c.x - 50);
            $("#popup").css("top", c.y - 190);
            var removeHandler = viewer.scene.postRender.addEventListener(function () {

                var changedC = Cesium.SceneTransforms.wgs84ToWindowCoordinates(viewer.scene, Checkposition);
                // If things moved, move the popUp too
                try {
                    if ((c.x !== changedC.x) || (c.y !== changedC.y)) {
                        //positionPopUp(changedC);

                        $("#popup").css("left", c.x - 50);
                        $("#popup").css("top", c.y - 210);
                        c = changedC;
                    }
                } catch (e) {
                }
            });
            // PopUp close button event handler
            $('#popup-closer1').on('click', function () {
                viewer.selectedEntity = undefined;
                viewer.trackedEntity = undefined;
                popUpCreated = false;
                $('#popup').hide();
                $('#popup-content').empty();
                removeHandler.call();
                return false;
            });
        }
    }
}

function getEntity(mousePosition) {
    var pickedObjects = scene.drillPick(mousePosition);
    var pickedObject;

    if (Cesium.defined(pickedObjects)) {

        pickedObject = pickedObjects[0];
    }
    return pickedObject;
}
